package com.test.cordova;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.cordova.base.BaseTitleActivity;
import com.test.cordova.citylist.view.LetterSideBar;
import com.test.cordova.model.City;
import com.test.cordova.model.Province;
import com.test.cordova.util.AesUtil;
import com.test.cordova.util.HexUtil;
import com.test.cordova.util.Logger;
import com.test.cordova.util.MD5Util;
import com.test.cordova.util.StatusBarUtil;
import com.test.cordova.view.CustomCircleImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 测试自定义控件
 */

public class CustomViewTestActivity extends BaseTitleActivity implements View.OnClickListener {

    private static final String TAG = CustomViewTestActivity.class.getSimpleName();
    @BindView(R.id.test_start_app)
    public Button test_start_app;
    @BindView(R.id.test_clip)
    public ImageView test_clip;
    @BindView(R.id.expand_lv_main)
    public ExpandableListView expand_lv_main;
    private List<Province> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Logger.e(TAG,"isInMultiWindowMode :"+isInMultiWindowMode());
        }

        data = new ArrayList<>();
        List<City> citys= new ArrayList<>();
        citys.add(new City(1,"广州市"));
        citys.add(new City(2,"茂名市"));
        citys.add(new City(3,"深圳市"));
        citys.add(new City(4,"惠州市"));
        citys.add(new City(5,"韶关市"));
        citys.add(new City(6,"东莞市"));
        data.add(new Province(1,"广东省",citys));
        data.add(new Province(2,"广东省1",citys));
        data.add(new Province(3,"广东省2",citys));
        data.add(new Province(4,"广东省3",citys));
        data.add(new Province(5,"广东省4",citys));
        expand_lv_main.setAdapter(new ExpandListViewAdapter());
        testClip();

    }

    public class ExpandListViewAdapter implements ExpandableListAdapter{

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public int getGroupCount() {
            return data.size();
        }
        // 二级级菜单的位置
        @Override
        public int getChildrenCount(int groupPosition) {
            return data.get(groupPosition).getCities().size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return data.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return data.get(groupPosition).getCities().get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return data.get(groupPosition).getId();
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return data.get(groupPosition).getCities().get(childPosition).getId();
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.expand_child,null,false);
                holder.cityName = convertView.findViewById(R.id.expand_child_name);
                holder.imageView = convertView.findViewById(R.id.expand_child_iv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (isExpanded) {
                //holder.imageView.setImageResource(android.R.drawable.arrow_up_float);
                /*ObjectAnimator rotation = ObjectAnimator.ofFloat(holder.imageView, "rotation", 0f,180f);
                rotation.setDuration(2000);
                rotation.start();*/
                holder.imageView.animate().rotation(180f).start();

            } else {
                //holder.imageView.setImageResource(android.R.drawable.arrow_down_float);
               /* ObjectAnimator rotation = ObjectAnimator.ofFloat(holder.imageView, "rotation", -180f,0f);
                rotation.setDuration(2000);
                rotation.start();*/
                holder.imageView.animate().rotation(-180f).start();
            }
            holder.cityName.setText(data.get(groupPosition).getName());
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.expand_child,null,false);
                holder.cityName = convertView.findViewById(R.id.expand_child_name);
                holder.imageView = convertView.findViewById(R.id.expand_child_iv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.imageView.setVisibility(View.GONE);
            holder.cityName.setText(data.get(groupPosition).getCities().get(childPosition).getName());
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void onGroupExpanded(int groupPosition) {

        }

        @Override
        public void onGroupCollapsed(int groupPosition) {

        }

        @Override
        public long getCombinedChildId(long groupId, long childId) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long groupId) {
            return 0;
        }

        class ViewHolder{
            private ImageView imageView;
            private TextView cityName;
        }
    }


    @Override
    public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
        Logger.e(TAG,"onMultiWindowModeChanged  isInMultiWindowMode"+isInMultiWindowMode +" newConfig :"+newConfig);
        super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
    }

    private int level = 0;
    private Handler clipHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                level += 1000;
                if (level < 10000) {
                    test_clip.getDrawable().setLevel(level);
                    clipHandler.sendEmptyMessageDelayed(0,1000);
                }
            }

        }
    };
    private void testClip() {
        clipHandler.sendEmptyMessage(0);
    }

    @Override
    protected void setListener() {
        test_start_app.setOnClickListener(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.custom_view_layout;
    }

    @Override
    protected String addTitle() {
        return "TestView";
    }

    @Override
    protected boolean isShowBack() {
        return true;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.test_start_app:

                String account = "e2eadmin";
                String cityid = "12";
                String timestamp = String.valueOf(System.currentTimeMillis());
                //获取MD5加密串 sign
                String sign = MD5Util.generatedCiphertext(account, "57415a582d4235355359362d", timestamp);

                String json = "{account:"+account+",password:'',cityId:"+cityid+",timestamp:"+timestamp+",sign:"+sign+"}";

                String desRS = AesUtil.ecbEnc(json, HexUtil.strCovered16("201803005ISMW"));
                Log.e("sssss",""+json);

                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName cn = new ComponentName("net.gdyuhui.wisdomcampd", "net.gdyuhui.wisdomcampd.WebViewActivity");
                intent.putExtra("from","GZZS");
                intent.putExtra("jumpInfos",desRS);
                intent.setComponent(cn);
                intent.setPackage("net.gdyuhui.wisdomcampd");
                startActivityForResult(intent, 1);

                break;
        }

    }
}
